package com.yf.dxapp.module.app.sms;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class CaptchaRedisDao {

    private String FORMATTER = "captcha_%s";

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public String getCaptcha(String phone){
        String redisKey = FORMATTER.formatted(phone);
        return stringRedisTemplate.opsForValue().get(redisKey);
    }

    /**
     * 三分钟有效
     * @param phone
     * @param captcha
     */
    public void setCaptcha(String phone,String captcha){
        String redisKey = FORMATTER.formatted(phone);
        stringRedisTemplate.opsForValue().set(redisKey,captcha,60*3, TimeUnit.SECONDS);
    }

}
